const express = require("express");
const router = express.Router();
const ResultJson = require("../model/ResultJson.js");
const path = require("path");
const fs = require("fs");
const multer = require("multer");
const serviceFactory = require("../ServiceFactory/ServiceFactory.js");
const upload = multer({
    dest:path.join(__dirname,"../adminPhoto")
})
const jwt = require("jsonwebtoken");
const AppConfig = require("../config/AppConfig.js");

// router.post("/uploadAdminPhoto",upload.single("admin_photo"),(req,resp) => {
//     let file = req.file;
//     if(file){
//         fs.renameSync(file.path,file.path + file.originalname);
//         resp.json(new ResultJson(true,"文件上传成功",`/adminPhoto/${file.filename + file.originalname}`))
//     }
// })

// router.post("/add",async (req,resp) => {
//     let results = await serviceFactory.adminInfoService.add(req.body);
//     resp.json(new ResultJson(results,results?"注册成功":"注册失败"))
// })

router.post("/login",async (req,resp) => {
    console.log('开始请求',req.body);
    
    let results = await serviceFactory.adminInfoService.checkLogin(req.body); 
    // console.log('res',results);
     
    if(results){
        let token = jwt.sign(
            {
                adminInfo:results
            },
            AppConfig.jwtKey,
            {
                expiresIn: 60 * 60 * 24,
                //令牌的过期事件，以秒为单位，过期时间步是按照发送时间算起，只要你不停的访问或者刷新页面，token的过期时间也会刷新
            }
        )
        // console.log(token)
        resp.json(new ResultJson(true,"登录成功",token))
    }else{
        resp.json(new ResultJson(false,"登录失败"))
    }  
})

module.exports = router;